﻿/*
Copyright 2010 Shane Ng

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using java.io;
using javax.servlet.http;

namespace Jeans {
    public class HttpServletResponseAdapter : HttpServletResponse {
        private HttpContext context;
        private HttpResponse Response {
            get {
                return context.Response;
            }
        }
        public HttpServletResponseAdapter(HttpContext context) {
            this.context = context;
        }


        public void addCookie(Cookie c) {
            Response.Cookies.Set(new HttpCookie(c.getName(), c.getValue()));
        }

        public void addDateHeader(string str, long l) {
            Response.AddHeader(str, new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(l).ToLongTimeString());

        }

        public void addHeader(string str1, string str2) {
            Response.AddHeader(str1, str2);
        }

        public void addIntHeader(string str, int i) {
            Response.AddHeader(str, i.ToString());
        }

        public bool containsHeader(string str) {
            return Response.Headers.AllKeys.Contains(str);
        }

        public string encodeRedirectURL(string str) {
            return context.Server.UrlPathEncode(str);
        }

        public string encodeRedirectUrl(string str) {
            return context.Server.UrlPathEncode(str);
        }

        public string encodeURL(string str) {
            return context.Server.UrlPathEncode(str);
        }

        public string encodeUrl(string str) {
            return context.Server.UrlPathEncode(str);
        }

        public void sendError(int i) {
            Response.StatusCode = i;
        }

        public void sendError(int i, string str) {
            Response.StatusCode = i;
            Response.StatusDescription = str;
        }

        public void sendRedirect(string str) {
            Response.Redirect(str);
        }

        public void setDateHeader(string str, long l) {
            Response.Headers[str] = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(l).ToLongTimeString();
        }

        public void setHeader(string str1, string str2) {
            Response.Headers[str1] = str2;
        }

        public void setIntHeader(string str, int i) {
            Response.Headers[str] = i.ToString();
        }

        public void setStatus(int i, string str) {
            Response.StatusCode = i;
            Response.StatusDescription = str;
        }

        public void setStatus(int i) {
            Response.StatusCode = i;
        }

        public void flushBuffer() {
            Response.Flush();
        }

        public int getBufferSize() {
            throw new NotImplementedException();
        }

        public string getCharacterEncoding() {
            return Response.ContentEncoding.EncodingName;
        }

        public string getContentType() {
            return Response.ContentType;
        }

        public java.util.Locale getLocale() {
            return new java.util.Locale(Response.Headers["Content-Language"]);
        }

        public javax.servlet.ServletOutputStream getOutputStream() {
            return new ServletOutputStreamAdapter(new JavaOutputStream(Response.OutputStream), Response.ContentEncoding);
        }

        public java.io.PrintWriter getWriter() {
            return new PrintWriter(new StreamWriter(context.Response.OutputStream, context.Response.ContentEncoding));
        }

        public bool isCommitted() {
            return Response.IsClientConnected;
        }

        public void reset() {
            Response.Clear();
            Response.ClearHeaders();
        }

        public void resetBuffer() {
            Response.ClearContent();
        }

        public void setBufferSize(int i) {
            Response.Buffer = true;
        }

        public void setCharacterEncoding(string str) {
            Response.ContentEncoding = Encoding.GetEncoding(str);
        }

        public void setContentLength(int i) {
            throw new NotImplementedException();
        }

        public void setContentType(string str) {
            Response.ContentType = str;
        }

        public void setLocale(java.util.Locale l) {
            Response.Headers["Content-Language"] = l.toString();
        }
    }
}